package com.fescoservice.service;

import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fescoservice.dao.CategoryDAO;
import com.fescoservice.dao.CompanyDAO;
import com.fescoservice.dao.CustomerDAO;
import com.fescoservice.dao.OperatorDAO;
import com.fescoservice.exception.FSException;
import com.fescoservice.model.Category;
import com.fescoservice.model.CategoryId;
import com.fescoservice.model.Company;
import com.fescoservice.util.MiscUtil;

@Service
public class CompanyService {
	private static final Logger log = LogManager.getLogger(CompanyService.class);
	@Autowired
	private CompanyDAO companyDAO;
	private CategoryDAO categoryDAO;
	@Autowired 
	CustomerDAO customerDAO;
	@Autowired 
	OperatorDAO operatorDAO;

	public void saveCategory(Category transientInstance){
		categoryDAO.save(transientInstance);
	}	

	public void updateCompany(final Company company) throws FSException{
		 StringBuffer hql = new StringBuffer(" update Company set  ");
			hql.append("compName = :compName,"); 	
			hql.append("compAddr = :compAddr,"); 
			hql.append("compZipcode = :compZipcode,");
			hql.append("compManager = :compManager,"); 	
			hql.append("compType = :compType,"); 	
			hql.append("compContact = :compContact,"); 	
			hql.append("compContactTel = :compContactTel where compId = :compId");
		companyDAO.update(hql.toString(),company);
	}	
	
	public void deleteCompany(Company company) throws FSException{		
		String hql = " delete from Company c where c.compId = :compId";
		System.out.println("company.getCompId():"+company.getCompId());
		System.out.println("L46  customerDAO.findByCompanyId(company.getCompId()).size():"+customerDAO.findByProperty("companyId",company.getCompId()).size());
		if(customerDAO.findByProperty("companyId",company.getCompId()).size()>0){
			throw new FSException("FS_ERROR_DELETE_COMPANY","");
		}
		if(operatorDAO.findByProperty("operCompanyId", company.getCompId()).size()>0){
			throw new FSException("FS_ERROR_DELETE_COMPANY_OPER","");
		}
		companyDAO.delete(hql,company);					
	}	
	
	public String findCompanyNameById(int id) throws FSException {
		String result = "";
			try {
				Company comp = companyDAO.findById(id);
				result = comp.getCompName();
			} catch (Exception e) {
				log.error(e.getMessage());
			}
		return result;
	}
	
	/**
	 * count(1)
	 * @param language
	 * @return
	 * @throws FSException
	 */
	public Integer countCompanies(final String compName)  throws FSException{
		final StringBuffer sql = new StringBuffer("SELECT COUNT(*) AS CNT FROM T_COMPANY C " );
		
		if(!MiscUtil.isNullOrEmpty(compName)){
			sql.append("  WHERE C.COMP_NAME like '%"+compName+"%'");
		}
		return companyDAO.countCompanies(sql.toString());
	}
	
	
	/**
	 * list
	 * @param language
	 * @return
	 * @throws FSException 
	 */
	public List<Company> getAllCompanies(final String compName) throws FSException{
		Company company = new Company();
		if(!MiscUtil.isNullOrEmpty(compName)){
			company.setCompName(compName);
		}
		
		StringBuffer sql = new StringBuffer("from Company c  ");
		if(!MiscUtil.isNullOrEmpty(compName)){
			sql.append(" where c.compName like '%'|| :compName ||'%'");
		}
		List list = companyDAO.findCompanyByName(sql.toString(),compName);

		return list;
	}
	
	
	/**
	 * 新增
	 * @param transientInstance
	 * @throws FSException 
	 */
	public void saveCompany(Company transientInstance) throws FSException{
		companyDAO.save(transientInstance);
	}	
	
	
	public Company findById(String id) {
		List list = companyDAO.findByProperty("compId",id);
		Company comp = (Company)list.get(0);
		 return comp;
	}
	
	public Company findCompanyById(String compId ) throws FSException{
		String sql = "from Company c where c.compId = :compId";
		List list = companyDAO.findCompanyById(sql,Integer.parseInt(compId));
		System.out.println("comp size:"+list.size());
		Company comp = (Company)list.get(0);
		 return comp;
	}
	
	public List<Company> findCompanyByName(final String compName,final int first) throws FSException{
		Company company = new Company();
		if(!MiscUtil.isNullOrEmpty(compName)){
			company.setCompName(compName);
		}
		
		StringBuffer sql = new StringBuffer("from Company c  ");
		if(!MiscUtil.isNullOrEmpty(compName)){
			sql.append(" where c.compName like '%'|| :compName ||'%'");
		}
		List list = companyDAO.findCompanyByName(sql.toString(),compName,first);
		return list;
	}
	
	public Long countCode1Name(String boardCode1) throws FSException{
		String sql = "SELECT COUNT(*) AS CNT FROM T_CATEGORY C WHERE C.BOARD_CODE1 ='"+boardCode1+"'";// ?";
		return categoryDAO.countCode1Name(sql,boardCode1);
	}
	
}
